using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace WinUITest.Pages
{
    [Menu("分组列表")]
    public sealed partial class GroupListPage : Page
    {
        public GroupListViewModel ViewModel { get; } = new();
        public GroupListPage()
        {
            InitializeComponent();
            ContactsCVS.Source = ViewModel.GetContactsGrouped();
        }
    }
    public partial class GroupListViewModel : ObservableObject
    {
        public ObservableCollection<GroupListDataViewModel> DataSource { get; } = [];
        public GroupListViewModel()
        {
            for (int i = 0; i < 1000; i++)
            {
                int groupIndex = i / 10;
                DataSource.Add(new GroupListDataViewModel() { Name = i.ToString(), GroupName = $"Group {groupIndex:000}" });
            }
        }
        public ObservableCollection<GroupInfoList> GetContactsGrouped()
        {
            IEnumerable<GroupInfoList> query = from item in DataSource.ToList()
                                               group item by item.GroupName into g
                                               orderby g.Key
                                               select new GroupInfoList(g) { Key = g.Key };
            return new ObservableCollection<GroupInfoList>(query);
        }
    }
    public partial class GroupListDataViewModel : ObservableObject
    {
        [ObservableProperty]
        public partial string Name { get; set; } = string.Empty;

        [ObservableProperty]
        public partial string GroupName { get; set; } = string.Empty;
    }
    public class GroupInfoList(IEnumerable<object> items) : List<object>(items)
    {
        public string Key { get; set; } = string.Empty;
        public override string ToString() => Key.ToString();
    }

}
